package com.codebuffer.nowcoder;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class Solution {
    ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        Queue<TreeNode> queue1 = new LinkedBlockingQueue();
        Queue<TreeNode> queue2 = new LinkedBlockingQueue();

        ArrayList list = new ArrayList();
        if(null==pRoot){
            return list;
        }
        queue1.add(pRoot);

        while(queue1.size()>0 || queue2.size()>0){

            List<Integer> listN = new ArrayList<>();
            Queue<TreeNode> queue11 = queue1.size()>0?queue1:queue2;
            Queue<TreeNode> queue22 = queue1.size()>0?queue2:queue1;

            while(queue11.size()>0){
                TreeNode poll = queue11.poll();
                listN.add(poll.val);
                if(null!=poll.left){
                    queue22.add(poll.left);
                }
                if(null!=poll.right){
                    queue22.add(poll.right);
                }

            }
            list.add(listN);

        }


        return list;
    }

}
